using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Sedna.Core.Controller;
using Sedna.Core.Controller.Configurations;
using Sedna.Core.Controls.Support;
using Sedna.Core.Controls.Support.Javascript;

namespace Sedna.Core.Controls
{
    public class ValidationMarker: BaseControl, IClientScriptingControl
    {
        private IList validators;
        private string textKey;

        #region Properties

        
        public new IValidationMarkerConfiguration Configuration
        {
            get { return (IValidationMarkerConfiguration) base.Configuration; }
            set { base.Configuration = value; }
        }
        
        public IList Validators
        {
            get { return validators; }
            set { validators = value; }
        }

        public string TextKey
        {
            get { return textKey; }
            set { textKey = value; }
        }

        public override bool ClientScriptingEnabled
        {
            get { return true; }
            set {  }
        }

        #endregion

        public ValidationMarker(IConfiguration configuration) : base(configuration)
        {
            this.Style["display"] = "none";
        }
        

        protected override void Build()
        {
            base.Build();
            TextKey = configuration.TextKey;
        }


        protected override void OnLoad(EventArgs e)
        {
            Validators = ResolveControls(Configuration.Validators);
            base.OnLoad(e);
        }

        protected override void RenderContents(HtmlTextWriter writer)
        {
//            if (EvaluateIsValid())
//            {
//                return;
//            }
            writer.Write(Configuration.GetLocalizedPostfixLabel(TextKey));
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            if (ClientScriptingEnabled)
            {
                DeclareScriptVariable();
            }
        }

        protected override IDictionary BuildScriptParameters()
        {
            IDictionary parameters = base.BuildScriptParameters();
            parameters.Add("validators", ScriptObjectGenerator.ToScript(GenerateValidatorScripts(), false));
            parameters.Add("isValid", ScriptObjectGenerator.ToScript(EvaluateIsValid()));
            return parameters;
        }

        #region Private methods
        private bool EvaluateIsValid()
        {
            if (validators == null)
            {
                return true;
            }
            foreach (IValidator validator in validators)
            {
                if (!validator.IsValid) return false;
            }
            return true;
        }


        private IList ResolveControls(IList ids)
        {
            IList controls = new ArrayList(ids.Count);
            foreach (string id in ids)
            {
                controls.Add(FindControl(id));
            }
            return controls;
        }
        
        private IList GenerateValidatorScripts()
        {
            IList validatorScripts = new ArrayList(validators.Count);
            foreach (Control control in validators)
            {
                validatorScripts.Add(JavascriptUtil.GetElementById(control.ClientID));
            }
            return validatorScripts;        
        }
        #endregion
    }
}
